﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.DomainServices.Client;
using NetBay.Core.BusinessEntities;
using Rio.Applications.Web.Services;
using Rio.Framework.Localisation;
using Rio.Framework.Pages.ModalControls;

namespace Rio.Framework.Pages
{
    public class Ventilate
    {
        List<long> m_oIdDocList;
        long m_lIdChrono = 0;
        long m_lUserId = 0;
        string m_sObject = string.Empty;
        string m_sMemo = string.Empty;
        string m_sTo = string.Empty;
        int m_iTypeFlow = 0;
        string m_sChronoWording;
        string m_sFolderURL;
        List<long> m_oSenderIds;
        Boolean m_bSendAlertByMail = false;
        Boolean m_bNotifyReceivers = true;
        private SplashScreen m_oSplashscreen = new SplashScreen();

        public event EventHandler VentilateDone;

        public Ventilate(List<long> p_oIdDocList, long p_lIdChrono, long p_lIdUser, string p_sObject, string p_sMemo, string p_sTo, Boolean p_bSendAlertByMail, Boolean p_bNotifyReceivers, int p_iTypeFlow = 0, List<long> p_oSenderIds = null, string p_sChronoWording = null, string p_sFolderURL = null)
        {
            m_oIdDocList = p_oIdDocList;
            m_lIdChrono = p_lIdChrono;
            m_lUserId = p_lIdUser;
            m_sObject = p_sObject;
            m_sMemo = p_sMemo;
            m_sTo = p_sTo;
            m_iTypeFlow = p_iTypeFlow;
            m_sChronoWording = p_sChronoWording;
            m_bSendAlertByMail = p_bSendAlertByMail;
            m_bNotifyReceivers = p_bNotifyReceivers;
            m_oSenderIds = p_oSenderIds;
            m_sFolderURL = p_sFolderURL;
        }

        public void Send()
        {
            m_oSplashscreen.Message = Resx.GetLabel(ResxCommon.SPLASHSCREEN_MESSAGE);
            m_oSplashscreen.Show();
            ModalDialogController.Launch(m_oSplashscreen);

            BusinessEntitiesDomainContext l_oBEDomainContext = new BusinessEntitiesDomainContext();
            ((WebDomainClient<BusinessEntitiesDomainContext.IBusinessEntitiesDomainServiceContract>)l_oBEDomainContext.DomainClient).ChannelFactory.Endpoint.Binding.SendTimeout = new TimeSpan(1, 0, 0);

            var AddMessageFlow_Query = l_oBEDomainContext.AddMessageFlowQuery(m_lUserId, m_lIdChrono, m_oIdDocList, m_sObject, m_sMemo, m_sTo + ";", m_bSendAlertByMail, m_bNotifyReceivers, m_iTypeFlow, m_sChronoWording, m_sFolderURL);
            l_oBEDomainContext.Load(AddMessageFlow_Query, OnAddMessageFlowQueryCallback, null);
        }
           

        private void OnAddMessageFlowQueryCallback(LoadOperation<ReturnLong> result)
        {
            List<ReturnLong> l_oRet = new List<ReturnLong>(result.Entities);

            try
            {
                ReturnLong l_oReturnLong = l_oRet.First();
                if (!l_oReturnLong.IsValid)
                {
                    MessageBoxChildWindow L_oMessageGetDocument = new MessageBoxChildWindow("", " -> OnAddMessageFlowQueryCallback failure Error : " + Environment.NewLine + l_oReturnLong.FullErrorMessage,
                                                                MessageBoxButtons.Ok, MessageBoxIcon.Error);
                    L_oMessageGetDocument.Show();
                }
                else
                {

                    if (m_oSenderIds != null)
                    {
                        BusinessEntitiesDomainContext l_oBEDomainContext = new BusinessEntitiesDomainContext();
                        ((WebDomainClient<BusinessEntitiesDomainContext.IBusinessEntitiesDomainServiceContract>)l_oBEDomainContext.DomainClient).ChannelFactory.Endpoint.Binding.SendTimeout = new TimeSpan(1, 0, 0);

                        l_oBEDomainContext.AddSendersToMessageFlow(m_oSenderIds, l_oReturnLong.Value);
                        
                    }
                    
                    if (VentilateDone != null)
                    {
                        VentilateDone(this, EventArgs.Empty);
                    }
                }

            }
            catch (Exception ex)
            {
                MessageBoxChildWindow L_oMessage = new MessageBoxChildWindow("", " -> OnAddMessageFlowQueryCallback failure Error : " + Environment.NewLine + ex.Message,
                                                                MessageBoxButtons.Ok, MessageBoxIcon.Error);
                L_oMessage.Show();
            }
            finally
            {
                m_oSplashscreen.Close();
                ModalDialogController.Close();
            }
        }

       
    }
}
